package com.wd.health.view.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.wd.health.R;
import com.wd.health.model.bean.ZxxqBean;
import com.wd.health.utils.RetrofitManager;
import com.wd.health.utils.Utils;

import java.text.SimpleDateFormat;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class ZxxqActivity extends AppCompatActivity {

    private WebView wv;
    private ImageView img;
    private TextView tv;
    private TextView time;
    private TextView name;
    private SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zxxq);
        initView();
        initData();
    }

    private void initData() {
        Intent intent = getIntent();
        int id = intent.getIntExtra("id", 0);
        RetrofitManager.getInstance().getZxxqData(id, new Observer<ZxxqBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(ZxxqBean zxxqBean) {
                String title = zxxqBean.getResult().getTitle();
                String source = zxxqBean.getResult().getSource();
                long releaseTime = zxxqBean.getResult().getReleaseTime();
                String content = zxxqBean.getResult().getContent();
                String format = simpleDateFormat.format(releaseTime);
                tv.setText(title);
                name.setText(source);
                time.setText(format);
                if (content.contains("http:http")) {
                    content = content.replaceAll("http:http", "http");
                }
                Utils.setttingWebView(wv);
                wv.loadDataWithBaseURL(null,content,"text/html","UTF-8",null);
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    private void initView() {
        img = findViewById(R.id.img_back);
        tv = findViewById(R.id.tv_zxxq_title);
        time = findViewById(R.id.tv_zxxq_time);
        name = findViewById(R.id.tv_zxxq_name);
        wv = findViewById(R.id.wv_zxxq);
        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}